#
#   user.py
#   journalismManger
#
#   Created by luoxh on 2019/8/13 - 17:38.
#   Copyright © 2019 luoxh. All rights reserved.
#
from mongoengine import DoesNotExist

from src.extension.exceptions import CustomException
from src.extension.status import CustomStatus
from src.model.user import User
from src.validator.common import (valid_user_password, valid_username, )


def valid_user_register(request):
    try:
        username = request.data['username']
        password = request.data['password']
    except KeyError: raise CustomException(CustomStatus.PARAMETER_ERROR)
    valid_user_password(password)
    valid_username(username)
    try: User.objects.get(username=username)
    except DoesNotExist: pass
    else: raise CustomException(CustomStatus.USERNAME_DOES_EXIST)
    return username, password
